﻿using System.Collections.Generic;

namespace HREngine.Bots
{
    public partial class Behavior骑士 : Behavior
    {
        public override string BehaviorName() { return "骑士"; }


        public override int getComboPenality(CardDB.Card card, Minion target, Playfield p)
        {
            // 无法选中
            if (target != null && target.untouchable)
            {
                return 100000;
            }
            // 初始惩罚值
            int pen = 0;
            int cost1 = 0;
            int cost2 = 0;
            if (p.ownMaxMana == 1)
                foreach (Handmanager.Handcard hc in p.owncards)
                {
                    if (hc.manacost == 1 && hc.card.type == CardDB.cardtype.MOB) cost1++;
                    else if (hc.manacost == 2) cost2++;
                }
            // 优先跳大哥
            if (cost2 == 1 && cost1 > 1 || cost2 > 1 && card.cost == 2 && p.enemyMinions.Count == 0)
            {
                return -50;
            }
            switch (card.chnName)
            {
                case "幸运币":
                    if (p.ownMaxMana > 1) break;
                    if (cost2 == 0 || (cost1 == 2 || cost1 == 3)) pen += 30;
                    if (cost2 == 1 && cost1 < 2) pen += 30;
                    break;
                //-----------------------------超模补正-----------------------------------
                case "蛛魔之卵":
                    foreach (Handmanager.Handcard hc in p.owncards)
                    {
                        if (hc.card.chnName == "阿达尔之手" || hc.card.chnName == "定罪" || hc.card.chnName == "王者祝福" || hc.card.chnName == "威能祝福")
                        {
                            foreach (Minion m in p.ownMinions)
                            {
                                if (m.handcard.card.chnName == "蛛魔之卵")
                                {
                                    return -5;
                                }
                            }
                            if (hc.manacost <= p.ownMaxMana + 1)
                            {
                                return -10;
                            }
                            break;
                        }
                    }
                    return -5;
                case "莫戈尔·莫戈尔格":
                case "双盾优等生":
                case "剧毒魔蝎":
                case "曼科里克":
                case "月牙":
                case "银色侍从":
                case "灵魂之匣":
                case "凯恩·血蹄":
                    return -8;
                case "泰兰·佛丁":
                    if (p.ownHero.Hp <= 10)
                    {
                        return -100;
                    }
                    if (p.enemyHero.Hp <= 8)
                    {
                        return -30;
                    }
                    return -8;
                //-----------------------------buff-----------------------------------
                case "威能祝福":
                case "阿达尔之手":
                case "王者祝福":
                case "智慧圣契":
                    if (target == null || target.untouchable) return 1000;
                    if (target.handcard.card.chnName == "甩笔侏儒" && !target.silenced) return 1000;
                    if (target.own && target.windfury) return -5;
                    if (target.own && target.cantAttack) return 20;
                    return -2;
                case "银色保卫者":
                    if (target == null || target.divineshild)
                    {
                        return 3;
                    }
                    return 0;
                //-----------------------------武器-----------------------------------
                case "逝者之剑":
                    if (p.ownWeapon.Durability > 0)
                    {
                        return 20;
                    }
                    // 脸被冻住了就别挂刀了
                    if (p.ownHero.frozen || p.ownSecretsIDList.Count > 2) return 20;
                    return -20;
                case "正义圣契":
                    pen = 5;
                    foreach (Minion m in p.enemyMinions)
                    {
                        pen -= m.Hp;
                    }
                    pen *= 10;
                    break;
                // return -60;     
                //-----------------------------针对卡-----------------------------------
                case "异教低阶牧师":
                    if (p.enemyHeroStartClass == TAG_CLASS.MAGE || p.enemyHeroStartClass == TAG_CLASS.PRIEST) return -20;
                    return 0;
                case "食人魔巫术师":
                    if (p.enemyMinions.Count == 0 && p.ownMinions.Count < 4)
                        return -39;
                    return -30;
                case "古神在上":
                    return -3;
                //-----------------------------配合-----------------------------------
                case "十字路口大嘴巴":
                    return -10 * p.ownSecretsIDList.Count;
                case "夺日者间谍":
                    return p.ownSecretsIDList.Count > 0 ? -5 : 1;
                case "银色自大狂":
                    foreach (Handmanager.Handcard hc in p.owncards)
                    {
                        if (hc.card.chnName == "威能祝福")
                        {
                            return 30;
                        }
                    }
                    break;
                case "前沿哨所":
                    // 前两个回合优先下，赌对面解不掉
                    if (p.ownMaxMana <= 2) return -100;
                    if (p.enemyHeroStartClass == TAG_CLASS.MAGE || p.enemyHeroStartClass == TAG_CLASS.PRIEST)
                        return -15;
                    return -10;
                case "防护长袍":
                    if (p.enemyHeroStartClass == TAG_CLASS.MAGE || p.enemyHeroStartClass == TAG_CLASS.PRIEST)
                        return -30;
                    return 0;
                case "火炮长斯密瑟":
                    return -20 * p.ownSecretsIDList.Count;
                case "北卫军指挥官":
                    return p.ownSecretsIDList.Count > 0 ? -20 : 1;
                case "复仇":
                    return -1;
                //-----------------------------英雄技能-----------------------------------
                case "援军":
                    return -1;
            }
            if (card.type == CardDB.cardtype.SPELL)
            {
                pen -= 2;
            }
            return pen;
        }

        private static int costVal = 10;
        // 抽牌价值
        private static int drawCardVal = 10;
        

        PenalityManager penman = PenalityManager.Instance;

        // 核心，场面值

        public override float getPlayfieldValue(Playfield p)
        {
            float retval = 0;
            retval += getGeneralVal(p);
            // 圣契奖励
            retval += p.libram > 2 ? 60 + p.libram * 10 : p.libram * 30;
            // 危险血线
            int hpboarder = 15;
            // 抢脸血线
            int aggroboarder = 20;
            retval += getHpValue(p, hpboarder, aggroboarder);
            // 出牌序列数量
            int count = p.playactions.Count;
            int ownActCount = 0;
            // 排序问题！！！！
            for (int i = 0; i < count; i++)
            {
                Action a = p.playactions[i];
                ownActCount++;
                switch (a.actionType)
                {
                    // 英雄攻击
                    case actionEnum.attackWithHero:
                        continue;
                    case actionEnum.useHeroPower:
                        continue;
                    case actionEnum.playcard:
                        break;
                    default:
                        continue;
                }
                switch (a.card.card.chnName)
                {
                    // 排序优先
                    case "凯瑞尔·罗姆":
                    case "圣礼骑士":
                    case "阿达尔之手":
                    case "新生入学":
                        retval -= i;
                        break;
                }
            }
            foreach (Handmanager.Handcard hc in p.owncards)
            {
                // 如果手上有侏儒就很赚
                if (hc.card.chnName == "甩笔侏儒")
                {
                    retval += 5;
                }
            }
            retval += getSecretPenality(p); // 奥秘的影响
            p.value = retval;
            return retval;
        }

        // 敌方随从价值 主要等于 
        public override int getEnemyMinionValue(Minion m, Playfield p)
        {
            // 休眠随从不考虑
            if (m.untouchable) return 0;

            int retval = 5;
            retval += m.Hp * 2;
            retval += m.handcard.card.spellpowervalue * 3;
            if (!m.frozen && !(m.cantAttack && m.name != CardDB.cardName.argentwatchman))
            {
                retval += m.Angr * 2;
                if (m.windfury) retval += m.Angr * 2;
                if (m.Angr > 4) retval += 20;
                if (m.Angr > 7) retval += 20;
            }

            if (!m.handcard.card.isSpecialMinion)
            {
                if (m.Angr == 0) retval -= 7;
                else if (m.Angr <= 2 && m.Hp <= 2 && !m.divineshild) retval -= 5;
            }
            else retval += m.handcard.card.rarity;

            if (m.taunt) retval += 5;
            if (m.divineshild) retval += m.Angr;
            if (m.divineshild && m.taunt) retval += 5;
            if (m.stealth) retval += 1;

            if (m.poisonous)
            {
                retval += 4;
                if (p.ownMinions.Count < p.enemyMinions.Count) retval += 10;
            }
            if (m.lifesteal) retval += m.Angr;

            if (m.handcard.card.targetPriority >= 1 && !m.silenced)
            {
                retval += m.handcard.card.targetPriority;
            }
            if (m.name == CardDB.cardName.nerubianegg && m.Angr <= 3 && !m.taunt) retval = 0;
            retval += m.synergy;


            switch (m.handcard.card.chnName){
                // 解不掉基本 gg
                case "科卡尔驯犬者":
                case "巡游领队":
                case "团队核心":
                case "前沿哨所":
                case "莫尔杉哨所":
                case "塔姆辛·罗姆":
                case "导师火心":
                case "伊纳拉·碎雷":
                case "暗影珠宝师汉纳尔":
                case "伦萨克大王":
                case "洛卡拉":
                    retval += 100;
                    break;
                // 我劝你最好解了
                case "食人魔巫术师":
                case "甩笔侏儒":
                case "精英牛头人酋长，金属之神":
                case "克欧雷":
                case "雷欧克":
                case "纳兹曼尼织血者":
                    retval += 50;
                    break;
                case "新生刺头":
                    if(m.Angr == 1)
                    {
                        retval += 50;
                    }
                    break;
                case "聒噪怪":
                    if(m.Spellburst)
                    {
                        retval += 50;
                    }
                    break;
            }
            return retval;
        }


        public override int getSirFinleyPriority(List<Handmanager.Handcard> discoverCards)
        {
            
            return -1; //comment out or remove this to set manual priority
            int sirFinleyChoice = -1;
            int tmp = int.MinValue;
            for (int i = 0; i < discoverCards.Count; i++)
            {
                CardDB.cardName name = discoverCards[i].card.name;
                if (SirFinleyPriorityList.ContainsKey(name) && SirFinleyPriorityList[name] > tmp)
                {
                    tmp = SirFinleyPriorityList[name];
                    sirFinleyChoice = i;
                }
            }
            return sirFinleyChoice;
        }

        public override int getMyMinionValue(Minion m, Playfield p)
        {
            int retval = 2;
            retval += m.Hp * 2;
            switch (m.handcard.card.chnName)
            {
                case "蛛魔之卵":
                    if (m.Angr > 0) retval += 30;
                    break;
            }
            if (!m.cantAttack || !m.Ready || !m.frozen)
            {
                retval += m.Angr;
            }
            else
            {
                retval += m.Angr / 4;
            }
            // 鱼人种族价值
            //if (m.handcard.card.race == 14)
            //{
            //    retval += m.Hp + m.Angr + 2;
            //}
            // 风怒价值
            if ((!m.playedThisTurn || m.rush == 1 || m.charge == 1) && m.windfury) retval += m.Angr;
            // 圣盾价值
            if (m.divineshild && m.handcard.card.chnName != "双盾优等生" ) retval += m.Angr / 2 + 1;
            // 潜行价值
            if (m.stealth) retval += m.Angr / 3 + 1;
            // 吸血
            if (m.lifesteal) retval += m.Angr / 3 + 1;
            // 圣盾嘲讽
            if (m.divineshild && m.taunt) retval += 4;
            // retval += m.synergy;
            // retval += getSpeicalCardVal(m.handcard.card);
            return retval;
        }

        private Dictionary<CardDB.cardName, int> SirFinleyPriorityList = new Dictionary<CardDB.cardName, int>
        {
            //{HeroPowerName, Priority}, where 0-9 = manual priority
            { CardDB.cardName.lesserheal, 0 }, 
            { CardDB.cardName.shapeshift, 6 },
            { CardDB.cardName.fireblast, 7 },
            { CardDB.cardName.totemiccall, 1 },
            { CardDB.cardName.lifetap, 9 },
            { CardDB.cardName.daggermastery, 5 },
            { CardDB.cardName.reinforce, 4 },
            { CardDB.cardName.armorup, 2 },
            { CardDB.cardName.steadyshot, 8 }
        };
        public override int getSirFinleyPriority(CardDB.Card card)
        {
            return SirFinleyPriorityList[card.name];
        }


    }
}